<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/10
 * Time: 14:33
 */

namespace app\admin\model;


class CategoryTitleRoom extends DataBase
{
    protected $name = 'category_title_room';
    protected $autoWriteTimestamp = true;

    public function labelRoom()
    {

        $title_id = input('get.title_id');
        $page_config = [
            'page' => input('page', 1),
            'list_rows' => 25,
            'query'=>[
                'title_id'=>$title_id
            ]
        ];
        // $data = $this->where()->select()->toArray();
        $data = $this->where('title_id','=',$title_id)->paginate($page_config);

        foreach($data as $key=>$value){

            $roomInfo =  Room::where('id','=',$value['room_id'])->field('r_id,r_name')->find();
            $data[$key]['r_name'] =$roomInfo['r_name'];
            $data[$key]['u_id'] = User::where('id','=',$roomInfo['r_id'])->value('u_id');
            $data[$key]['t_name'] = CategoryTitle::where('id','=',$value['title_id'])->value('name');
        }

        return $data;
    }

    public function labelRoomChangeStatus()
    {
        $id = input('get.id');
        if(empty($id)) return ['code'=>-100,'msg'=>'传参错误'];

        $status = $this->where('id','=',$id)->value('status');
        $status = $status == 1 ? 0 : 1;
        $res = $this->save(['status'  => $status],['id' => $id]);

        if(!empty($res)) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

    public function labelRoomAdd($u_id,$title_id)
    {
        $r_id = User::where('u_number','=',$u_id)->value('user_id');
        $room_id = Room::where('user_id','=',$r_id)->value('id');
        if(empty($room_id))  return ['code'=>-100,'msg'=>'房间不存在'];

        $res = $this->where([['room_id','=',$room_id],['title_id','=',$title_id]])->find();
        if(!empty($res)) return ['code'=>-100,'msg'=>'标签房间已存在'];

        $re = $this->save(['room_id'=>$room_id,'title_id'=>$title_id]);
        if($re) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];
    }

}